// pages/projects/projectsindex.js
var category = require('../../template/category/category.js');
var allgoods
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperinfo: {
      hgt: 0,
      wtd: 0,
      indicatorDots: true,
      autoplay: true,
      circular: true,
      interval: 5000,
      duration: 500,
      curpage: 0,
      isLast: false,
      goodstype: 0,
      isInitcategory: false
    },
    showModel: false,
    banners: [
      {
        imgurl: '../imgs/projects/banner1.jpg',
        name: '' //初颜系列-微精华 亲呵护
      },
      {
        imgurl: '../imgs/projects/banner2.jpg',
        name: '' //初颜系列-微精华 亲呵护
      }
    ],
    swiperCurrent: 0,
    listhgt: 0,
    goods_width: 49,
    goods_pic_width: 100,
    goods_pic_rest_width: 100,
    topNeedShow: true,
    goodslist:[],
    goodsTplist:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var swiper_hgt = 0 * res.windowWidth / 1000;
        var swiperinfo = that.data["swiperinfo"];
        swiperinfo.hgt = swiper_hgt;
        var blankhgt = 46 + swiper_hgt;
        var goods_height = res.windowWidth * that.data.goods_width * 0.01 * that.data.goods_pic_width * 0.01;
        var tmphgt = res.windowHeight;

        that.setData({
          swiperinfo: swiperinfo,
          blankhgt: blankhgt,
          listhgt: tmphgt,
          goods_pic_height: goods_height
        });
      },
    });


    console.log('im goods list');
  },
  loadGoodsToServer: function () {
    var url = app.globalData.webUrl + 'Goods/goods_list';

    var tdata = { 'type': 1, goodstp: this.data.goodstype, 'page': this.data.curpage, 'storeid': app.globalData.storeid };

    app.loadDataFromServer(url, this.loadCallBack, tdata);
  },
  loadCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: '网络错误~',
        image: '../imgs/tips.png'
      });
      return;
    }

    // console.log(res);

    var goodslist = res.result.arr;
    for (var i = 0; i < goodslist.length; i++) {
      goodslist[i].icon = app.globalData.ImgwebUrl + goodslist[i].icon;
    }

    if (!this.data.isInitcategory) {
      var tparr = res.result.tparr;
      tparr.unshift({ typename: '所有项目', name: '所有项目', id: 0 });
      for (var i = 0; i < tparr.length; i++) {
        tparr[i].name = tparr[i].typename;
      }
      category.init(this, tparr, this.data.blankhgt, this.selectCategoryCallBack);
      this.data.isInitcategory = true;
    }

    this.data.isLast = true;
    if (goodslist.length == app.globalData.pageSize) {
      this.data.curpage += 1;
      this.data.isLast = false;
    }
    
    if (this.data.curpage > 0) {
      var orgData = this.data.goodslist;
      for (var i = 0; i < goodslist.length; i++) {
        orgData.push(goodslist[i]);
      }
      this.setData({
        goodslist: orgData
      });
    }
    else {
      this.setData({
        goodslist: goodslist
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.goodslist = [];
    this.clearGoodsPageDb();
    this.loadGoodsToServer();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    return app.getShareObj();
  },
  selectCategoryCallBack: function (res) {
    console.log(res);
    if (res.id != this.data.goodstype){
      this.data.goodslist = [];
    }
    category.close();
    this.clearGoodsPageDb();
    this.data.goodstype = res.id;
    this.loadGoodsToServer();
  },
  clearGoodsPageDb: function () {
    this.data.curpage = 0;
    this.data.isLast = false;
    // this.data.goodstype = 0;
    this.setData({
      scrolltop: 0
    });
  },
  loadMoreGoods: function () {
    if (!this.data.isLast)
      this.loadGoodsToServer();
  },
  goodsScroll: function (e) {
    return;
    var tmphgt = this.data.blankhgt;
    var topNeedShow = true;
    if (e.detail.deltaY < 0 && e.detail.scrollTop > tmphgt) {
      topNeedShow = false;
    }
    this.setData({
      topNeedShow: topNeedShow
    });
  },
  goodsTap: function (e) {
    var goodsid = e.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: './detail/detail?goodsid=' + goodsid,
    });
  },
  // ------------- category call back-----------
  showCategory: category.show,
  closeCategory: category.close,
  chooseCategory: category.choose
})